package com.dm0858.bianmin.mine.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;

import com.dm0858.bianmin.R;
import com.dm0858.bianmin.databinding.ActivityPostManageLayoutBinding;
import com.dm0858.bianmin.mine.adapter.MyReleaseRecyclerViewAdapter;
import com.dm0858.bianmin.mine.adapter.PostManageRecyclerViewAdapter;
import com.dm0858.bianmin.mine.base.BaseActivity;
import com.dm0858.bianmin.mine.model.MineModel;
import com.dm0858.bianmin.mine.network.Retrofit;
import com.dm0858.bianmin.mine.network.RetrofitListener;
import com.dm0858.bianmin.mine.viewmodel.MyReleaseViewModel;
import com.dm0858.bianmin.mine.viewmodel.PostManagerViewModel;

public class PostManageActivity extends BaseActivity implements View.OnClickListener {

    private ActivityPostManageLayoutBinding binding;

    private PostManagerViewModel viewModel;


    private PostManageRecyclerViewAdapter adapter;

    private int page = 1;

    private Retrofit retrofit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_post_manage_layout);
        binding.setOnClick(this);
        initData();
    }

    @Override
    protected void initData() {
        if (retrofit == null) retrofit = new Retrofit();
        viewModel = new ViewModelProvider(this).get(PostManagerViewModel.class);
        viewModel.userArticle(1);
        viewModel.getReleaseList().observe(this, dataBeans -> {
            if (dataBeans == null) return;
            if (adapter == null) {
                adapter = new PostManageRecyclerViewAdapter(this,R.layout.adapter_post_manage_item_layout,dataBeans);
                binding.recyclerview.setLayoutManager(new GridLayoutManager(this,1));
                binding.recyclerview.setAdapter(adapter);
            } else {
                adapter.setList(dataBeans);
            }
            adapter.setOnLongClickListener(position -> {
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
                dialogBuilder
                        .setTitle("删除帖子")
                        .setMessage("确定要删除帖子吗？")
                        .setCancelable(true)
                        .setPositiveButton("确定", (dialogInterface, i) ->{
                            MineModel.delArticle(retrofit, dataBeans.get(position).getId(), new RetrofitListener() {
                                @Override
                                public void onSuccess(Object object) {
                                    dataBeans.remove(position);
                                    adapter.setList(dataBeans);
                                }

                                @Override
                                public void onError(int messageCode, String e) {

                                }
                            });
                        }).setNegativeButton("取消", (dialogInterface, i) -> {

                });
                AlertDialog dialog = dialogBuilder.create();
                dialog.show();
            });


        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                onBackPressed();
                break;
        }
    }
}